const { resolve } = require('path')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')

const DefineCssExtractPlugin = new MiniCssExtractPlugin({
  filename: 'styles/[name].[contenthash:6].css'
})

const loaderOptions = {
  loaders: {
    css: ['vue-style-loader', { loader: 'css-loader', options: { sourceMap: true } }],
    postcss: ['vue-style-loader', { loader: 'css-loader', options: { sourceMap: true } }],
    less: ['vue-style-loader', { loader: 'css-loader', options: { sourceMap: true } }, { loader: 'less-loader', options: { sourceMap: true } }],
    sass: ['vue-style-loader', { loader: 'css-loader', options: { sourceMap: true } }, { loader: 'sass-loader', options: { indentedSyntax: true, sourceMap: true } }],
    scss: ['vue-style-loader', { loader: 'css-loader', options: { sourceMap: true } }, { loader: 'sass-loader', options: { sourceMap: true } }],
    stylus: ['vue-style-loader', { loader: 'css-loader', options: { sourceMap: true } }, { loader: 'stylus-loader', options: { sourceMap: true } }],
    styl: ['vue-style-loader', { loader: 'css-loader', options: { sourceMap: true } }, { loader: 'stylus-loader', options: { sourceMap: true } }]
  },
  cssSourceMap: true,
  cacheBusting: true,
  transformToRequire: {
    video: ['src', 'poster'],
    source: 'src',
    img: 'src',
    image: 'xlink:href'
  }
}

module.exports = {
  mode: 'development',
  entry: {
    app: './src/main.js'
  },
  output: {
    path: resolve(__dirname, 'dist'),
    filename: 'scripts/[name].[chunkhash:6].js',
    publicPath: '/'
  },
  module: {
    rules: [
      // {
      //     enforce: 'pre',
      //     test: /\.(js|vue)$/,
      //     loader: 'eslint-loader',
      //     exclude: /node_modules/
      // },
      {
        test: /\.vue$/,
        use: [{
          loader: 'vue-loader',
          // options: {
          //     // extractCSS: DefineCssExtractPlugin
          //     extractCSS: true,
          //     css: [DefineCssExtractPlugin, 'css-loader'],
          // }
          options: loaderOptions
        }]
      },
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          // 'vue-style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.less$/,
        use: [
          MiniCssExtractPlugin.loader,
          // 'vue-style-loader',
          'css-loader',
          'less-loader'
        ]
      },

      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      },
      {
        test: /\.(jpg|png|gif|svg)$/,
        use: [{
          loader: 'url-loader',
          options: {
            esModule: false, // 不加的话会有这种情况,img属性src="[object Module]"
            limit: 10240,
            name: 'images/[name].[hash:6].[ext]'
          }
        }]
      },
      {
        test: /\.(woff2?|eot|ttf|otf)$/,
        loader: 'url-loader',
        options: {
          esModule: false,
          limit: 10240,
          name: 'fonts/[name].[hash:6].[ext]'
        }
      }
    ]
  },
  optimization: {
    splitChunks: {
      chunks: 'all', // 默认只作用于异步模块，为`all`时对所有模块生效,`initial`对同步模块有效
      minSize: 30000, // 合并前模块文件的体积
      minChunks: 1, // 最少被引用次数
      name: true,
      cacheGroups: {
        vendors: {
          chunks: 'all',
          test: /[\\/]node_modules[\\/]/,
          name: 'vendor',
          minChunks: 1, // 最少被引用次数
          priority: -10 // 优先级更高
        }
      }
    }
  },
  plugins: [
    new CleanWebpackPlugin(),
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      title: 'vue-app-base',
      template: './index.html',
      filename: 'index.html'
    }),
    DefineCssExtractPlugin,
    new webpack.DefinePlugin({
      BASE_URL: JSON.stringify('./')
    }),
    new CopyWebpackPlugin({
      patterns: [{
        from: resolve(__dirname, 'public'),
        to: resolve(__dirname, 'dist')
      }]
    })
  ],
}
